import { Component, OnInit, Output, EventEmitter, ViewChild } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Service } from '../../../server/server';
import { CudTableService } from '../../../public/serve/cud-table/cud-table.service';
declare var $: any;
interface Reponse {
	result_code: string
}

@Component({
	selector: 'app-privilege-newly-added',
	templateUrl: './privilege-newly-added.component.html'
})
export class PrivilegeNewlyAddedComponent implements OnInit {

	constructor(private http: HttpClient, private server: Service, private cudTableService: CudTableService) { }

	ngOnInit() {

	}

	@Output()
	addOutput: EventEmitter<any> = new EventEmitter();
	@ViewChild('newlyAddedForm') formElement: HTMLFormElement

	public initial: boolean = true;

	submitNewlyAdded(val) {
		let data: NewlyAdded = new NewlyAdded(val.operationCode, val.operationName, Number(val.enable), val.description);
		//console.log(data)
		let url = this.server.baseUrl+'admin/operations/add';
		this.cudTableService.postRequest(url, data).then((res: any) => {
			if (res.result_code == '0000') {
				$("#add").modal("hide");
				//prompt
				this.server.prompt = { prompt: true, success: true, fail: false, promptStatement: res.message };
				this.server.promptclose;
				this.addOutput.emit(res.result_code);
				this.reset();
				//update
			} else {
				this.server.prompt = { prompt: true, success: false, fail: true, promptStatement: res.message };
				this.server.promptclose;
			}
		}).catch();
	}

	//清空表单
	reset() {
		//console.log(val)
		this.formElement.reset()
		setTimeout(() => {
			this.initial = true;
		}, 300)
	}
}

export class NewlyAdded {
	constructor(
		public operationCode: string,
		public operationName: string,
		public enable: any,
		public description?: string
	) { }
}